from selenium import webdriver
from zhilian.settings import ROOT_DIR
import os
from utils.cookies_get import make_cookie
from time import sleep
import json
from test.settings import ZHIHU_COOKIE_FILE

path =os.path.join(ROOT_DIR,'chromedriver')
browser = webdriver.Chrome(executable_path=path)
# cookies = make_cookie()
#访问知乎首页
browser.get('http://www.zhihu.com')
sleep(5)
#点击登录按钮
browser.find_element_by_css_selector('div.SignContainer-switch span').click()
sleep(3)
buttons = browser.find_elements_by_class_name("Button")
for i in buttons:
    if i.text == "二维码登录":
        i.click()
        break

sleep(10)
#下面是点击账户密码登录的效果
# browser.find_element_by_css_selector("input[name='username']").send_keys('13308047191')
# browser.find_element_by_css_selector("input[name='password']").send_keys('tusbasa1')
# sleep(1)
# browser.find_element_by_css_selector("button.SignFlow-submitButton").click()
# sleep(20)
# browser.find_element_by_css_selector("button.SignFlow-submitButton").click()
# sleep(2)
# browser.find_element_by_css_selector("button.SignFlow-submitButton").click()
# sleep(5)



# for data in cookies.items():
#     name,value = data
#     print(name,value)
#
#     browser.add_cookie(cookie_dict={'name':name,'value':value})
cookie = browser.get_cookies()
cookie_str = json.dumps(cookie)
with open(ZHIHU_COOKIE_FILE,'w') as f:
    f.write(cookie_str)
print(cookie)
browser.close()